package net.arvin.dao;

import net.arvin.mobilebanksystem.R;
import net.arvin.model.TransfersBill;
import net.arvin.utils.PhotoUtil;
import net.arvin.utils.TransfersType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TransferDetailsAdapter extends BaseAdapter {
	private TransfersBill mBill;
	private LayoutInflater mInflater;
	private Context context;

	public TransferDetailsAdapter(TransfersBill mBill, Context context) {
		this.mBill = mBill;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mBill.getCardOrUser().size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listitem_transfer_details,
					null);
			holder = new ViewHolder();
			holder.leftLayout = (RelativeLayout) convertView
					.findViewById(R.id.left_layout);
			holder.left_userIcon = (ImageView) convertView
					.findViewById(R.id.recieve_user_icon);
			holder.left_detailTv = (TextView) convertView
					.findViewById(R.id.recieve_detail_tv);
			holder.left_time = (TextView) convertView
					.findViewById(R.id.left_time);
			holder.right_userIcon = (ImageView) convertView
					.findViewById(R.id.send_user_icon);
			holder.rightLayout = (RelativeLayout) convertView
					.findViewById(R.id.right_layout);
			holder.right_detailTv = (TextView) convertView
					.findViewById(R.id.send_detail_tv);
			holder.right_time = (TextView) convertView
					.findViewById(R.id.right_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mBill.getCardOrUser().get(position).equals(TransfersType.U2U)
				|| mBill.getCardOrUser().get(position)
						.equals(TransfersType.U2C)) {
			if (mBill.getRecieveOrSend().get(position)
					.equals(TransfersType.SEND)) {
				holder.leftLayout.setVisibility(View.GONE);
				holder.rightLayout.setVisibility(View.VISIBLE);
				holder.right_userIcon.setImageResource(R.drawable.avatar);
				if(PhotoUtil.getAvatar(context)!=null){
					holder.right_userIcon.setImageBitmap(PhotoUtil.getAvatar(context));
				}
				holder.right_detailTv.setText("转了"
						+ mBill.getMoney().get(position));
				holder.right_time.setText(mBill.getTime().get(position));
			} else {
				holder.rightLayout.setVisibility(View.GONE);
				holder.leftLayout.setVisibility(View.VISIBLE);
				holder.left_userIcon.setImageResource(R.drawable.avatar);
				holder.left_detailTv.setText("转了"
						+ mBill.getMoney().get(position));
				holder.left_time.setText(mBill.getTime().get(position));
			}
		} else {
			if (mBill.getRecieveOrSend().get(position)
					.equals(TransfersType.SEND)) {
				holder.leftLayout.setVisibility(View.GONE);
				holder.rightLayout.setVisibility(View.VISIBLE);
				holder.right_userIcon.setImageResource(R.drawable.bank_icon);
				holder.right_detailTv.setText("转了"
						+ mBill.getMoney().get(position));
				holder.right_time.setText(mBill.getTime().get(position));
			} else {
				holder.rightLayout.setVisibility(View.GONE);
				holder.leftLayout.setVisibility(View.VISIBLE);
				holder.left_userIcon.setImageResource(R.drawable.bank_icon);
				holder.left_detailTv.setText("转了"
						+ mBill.getMoney().get(position));
				holder.left_time.setText(mBill.getTime().get(position));
			}
		}
		return convertView;
	}

	class ViewHolder {
		RelativeLayout leftLayout;
		ImageView left_userIcon;
		TextView left_detailTv;
		TextView left_time;
		RelativeLayout rightLayout;
		ImageView right_userIcon;
		TextView right_detailTv;
		TextView right_time;
	}
}
